package entity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * Values and methods for specifying the DTEND property
 * for the iCalendar object as defined in RFFC 5545. 
 * This property specifies the date and time that a 
 * calendar component ends.
 * 
 * @author Sean Young
 * @version 1.0
 *
 */
public class DateTimeEnd {
	/** A parameter for the DTEND property that specifies a value. Format must be DATE-TIME or DATE */
	private String valueparam;
	
	/** A parameter for the DTEND property that specifies a tzid (Timezone ID) */
	private String tzidparam;
	
	/** A string containing other parameters for the DTEND property. */
	private String otherparam;
	
	/** The value of the DTEND property. Value must be DATE-TIME or DATE */
	private String dtendval;
	
	/**
	 * Constructor for the Date-Time END property. All fields are defaulted
	 * to null.
	 */
	public DateTimeEnd() {
		this.valueparam = null;
		this.tzidparam = null;
		this.otherparam = null;
		this.dtendval = null;
	}
	
	/**
	 * Sets the value of the value TZID parameter field.
	 * @param value The value parameter. Must be DATE or DATE-TIME.
	 */
	public void setValueParam(String value) {
		this.valueparam = " VALUE=" + value;
	}
	
	/**
	 * Sets the value of the TZID parameter field. The value must
	 * be a valid tzid (Timezone ID).
	 * @param value The tzid
	 */
	public void setTZIDParam(String value) {
		this.tzidparam = value;
	}
	
	/**
	 * Sets the value of the "other" parameter field.
	 * @param otherparam The "other" parameter. Must be a single
	 *                   parameter.
	 */
	public void setOtherParam(String otherparam) {
		this.otherparam = otherparam;
	}
	
	/**
	 * Sets the value of the "Other" parameter field. 
	 * @param otherparams A List containing the parameters to be added
	 *                    to the "other" parameter.
	 */
	public void setOtherParam(List<String> otherparams) {
		StringBuilder parameterBuilder = new StringBuilder();
		for(int i = 0; i < otherparams.size(); i++) {
			parameterBuilder.append(otherparams.get(i));
			if((otherparams.size() != 1) && (i != otherparams.size() - 1)) parameterBuilder.append(";");
		}
		this.otherparam = parameterBuilder.toString();
	}
	
	/**
	 * Sets the end date value of the DTEND field.
	 * @param d The end date
	 */
	public void setEndDate(Date d) {
		SimpleDateFormat dForm = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
		dForm.setTimeZone(TimeZone.getTimeZone("GMT"));
		this.dtendval = dForm.format(d);
	}
	
	/**
	 * Returns a string containing the values of the fields of the
	 * DTEND property. If no parameters were specified then they 
	 * will not be included. If no end date was specified then a null
	 * will be returned.
	 *  @return A string containing the properly formatted DTEND property
	 */
	public String toString() {
		StringBuilder dtendString = new StringBuilder("DTEND");
		if(dtendval == null) return null;
		if(valueparam != null) dtendString.append(valueparam);
		if(tzidparam != null) {
			if(valueparam != null) dtendString.append(";");
			dtendString.append(tzidparam);
		}
		if(otherparam != null) {
			if(valueparam != null || tzidparam != null) dtendString.append(";");
			dtendString.append(otherparam);
		}
		
		dtendString.append(":");
		
		dtendString.append(dtendval);
		dtendString.append("\r\n");
		
		return dtendString.toString();
	}
}
